﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

namespace KPIS.GERP.GBMS.Reports
{
    public partial class RPT_Exports : System.Web.UI.Page
    {
        DataTable dt = null;
        
        public void Page_Load(object sender, EventArgs e)
        {

        }

        //protected override void OnLoad(System.EventArgs e)
        //{
        //    //base.OnLoad(e);

        //    if (!IsPostBack)
        //    {
        //        if (!Object.Equals(Session["ReportName"], null))
        //        {
        //            string ReportName = Session["ReportName"].ToString();
        //            LoadReport(ReportName);
        //        }
        //    }

        //    CrystalReportViewer1.ReportSource = (CrystalDecisions.CrystalReports.Engine.ReportDocument)this.Session["ReportSource"];

        //}

        protected void CrystalReportViewer1_Init(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (!Object.Equals(Session["ReportName"], null))
                    {
                        string ReportName = Session["ReportName"].ToString();
                        LoadReport(ReportName);
                    }
                }

                CrystalReportViewer1.ReportSource = (CrystalDecisions.CrystalReports.Engine.ReportDocument)this.Session["ReportSource"];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void LoadReport(string ReportName)
        {
            try
            {
                if (Object.Equals(dt, null))
                    dt = (DataTable)Session["dtReport"];

                CrystalDecisions.CrystalReports.Engine.ReportDocument rpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                rpt.Load(Server.MapPath("~/Reports/" + ReportName + ".rpt"));

                //โหลดข้อมูล เข้า SubReport
                if (Session["dtSubReport"] != null)
                {
                    string[] strSubReportName = (string[])Session["strSubReportName"];
                    DataTable[] dtSubReport = (DataTable[])Session["dtSubReport"];

                    for (int i = 0; i < rpt.Subreports.Count; i++)
                    {
                        rpt.OpenSubreport(strSubReportName[i].ToString() + ".rpt");
                        rpt.Subreports[strSubReportName[i].ToString() + ".rpt"].SetDataSource(dtSubReport[i].DefaultView);
                    }
                    //Session["dtSubReport"] = null;
                }

                //โหลดข้อมูล เข้า MainReport
                rpt.SetDataSource(dt.DefaultView);
                //Session["dtReport"] = null;
                if (Session["dtReportParam"] != null)
                {
                    DataTable dtReportParam = (DataTable)Session["dtReportParam"];

                    for (int i = 0; i < dtReportParam.Rows.Count; i++)
                    {
                        if (dtReportParam.Rows[i]["SubReportName"].ToString() != "")
                        {
                            rpt.SetParameterValue(dtReportParam.Rows[i]["ParamName"].ToString(), dtReportParam.Rows[i]["ParamValue"].ToString(), dtReportParam.Rows[i]["SubReportName"].ToString());
                        }
                        else
                        {
                            rpt.SetParameterValue(dtReportParam.Rows[i]["ParamName"].ToString(), dtReportParam.Rows[i]["ParamValue"].ToString());
                        }
                    }

                    //Session.Remove("dtReportParam");
                }

                rpt.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, string.Format("202-{0}", DateTime.Now.ToString("yyyyMMddHHmmssffff")));
                //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                //Response.AddHeader("content-disposition", string.Format("attachment;  filename={0}.xlsx", string.Format("202-{0}", DateTime.Now.ToString("yyyyMMddHHmmssffff"))));
                //Response.BinaryWrite(.GetAsByteArray());
                //rpt.ExportToDisk(ExportFormatType.Excel, Server.MapPath("~/Reports/Temp/"));

                ////เก็บ ReportSource บน Session
                //this.Session["ReportSource"] = rpt;

                //CrystalReportViewer1.ReportSource = (CrystalDecisions.CrystalReports.Engine.ReportDocument)this.Session["ReportSource"];
                ////CrystalReportViewer1.EnableDatabaseLogonPrompt = false;
                //CrystalReportViewer1.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
